<template>
    <div>
        <!--        面包屑导航栏-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <!--            添加分类按钮-->
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!--            表格-->
            <!--            第三方插件-->
            <tree-table :data="cateList" :columns="columns" class="treeTable"
                        :selection-type="false"
                        :expand-type="false"
                        show-index
                        index-text="#"
                        border
            >
                <!--                是否有效-->
                <template slot-scope="scope" slot="isok">
                    <i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted === false"></i>
                    <i class="el-icon-error" style="color: red" v-else></i>
                </template>
                <!--                排序-->
                <template slot="order" slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
                    <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                    <el-tag size="mini" type="warning" v-else>三级</el-tag>
                </template>
                <!--                操作-->
                <template slot="opt" slot-scope="scope">
                    <el-button icon="el-icon-edit" type="primary" @click="showEditCateDialog(scope.row)">编辑</el-button>
                    <el-button icon="el-icon-delete" type="danger" @click="deleteCate(scope.row)">删除</el-button>
                </template>
            </tree-table>
            <!--            分页-->
            <el-pagination class="page"
                           @size-change="handleSizeChange"
                           @current-change="handleCurrentChange"
                           :current-page="queryInfo.pagenum"
                           :page-sizes="[3,5,8,10]"
                           :page-size="queryInfo.pagesize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </el-card>
        <!--        添加分类对话框-->
        <el-dialog
                title="添加分类"
                @close="addCateDialogClosed"
                :visible.sync="addCateDialogVisible"
                width="40%"
        >
            <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类">
                    <!--                    级联选择器-->
                    <el-cascader
                            clearable

                            v-model="selectedIds"
                            :options="parentCateList"
                            :props="cateListProps"
                            @change="parentCateListChange"></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="addCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
        </el-dialog>
        <!--        编辑分类对话框-->
        <el-dialog
                title="编辑分类"
                @close="editCateDialogClosed"
                :visible.sync="editCateDialogVisible"
                width="40%"
        >
            <el-form :model="editCateForm" :rules="editCateFormRules" ref="editCateFormRef" label-width="100px">
                <el-form-item label="分类名称" prop="cat_name">
                    <el-input v-model="editCateForm.cat_name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
    <el-button @click="editCateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editCate">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Cate",
        data() {
            return {
                cateList: [],
                total: 0,
                queryInfo: {
                    type: 3,
                    pagenum: 1,
                    pagesize: 5
                },
                columns: [
                    {
                        label: '分类名称',
                        prop: 'cat_name',
                        headerAlign:'center'
                    }, {
                        label: '是否有效',
                        // 自定义模板列
                        type: 'template',
                        // 自定义模板的名称
                        template: 'isok',
                        align:'center',
                        headerAlign:'center'
                    }, {
                        label: '排序',
                        type: 'template',
                        template: 'order',
                        align:'center',
                        headerAlign:'center'
                    }, {
                        label: '操作',
                        type: 'template',
                        template: 'opt',
                        align:'center',
                        headerAlign:'center'

                    }
                ],
                // 添加分类
                addCateDialogVisible: false,
                addCateForm: {
                    cat_name: '',
                    cat_level: 0,
                    // 父级分类ID
                    cat_pid: 0,
                },
                addCateFormRules: {
                    cat_name: [
                        {required: true, message: '请输入分类名称', trigger: 'blur'},
                    ]
                },
                parentCateList: [],
                // 级联选择器
                selectedIds: [],
                // 配置
                cateListProps: {
                    label: 'cat_name',
                    value: 'cat_id',
                    children: 'children',
                    expandTrigger: 'hover',
                    checkStrictly: true
                },
                // 编辑分类
                editCateDialogVisible: false,
                editCateForm: {
                    cat_name: '',
                    cat_id: ''
                },
                editCateFormRules: {
                    cat_name: [
                        {required: true, message: '请输入分类名称', trigger: 'blur'},
                    ]
                }
            }
        },
        created() {
            this.getCateList()
        },
        methods: {
            async getCateList() {
                const res = (await this.$http.get('categories', {params: this.queryInfo})).data
                if (res.meta.status !== 200) {
                    this.$message.error({
                        message: '请求商品分类列表失败!',
                        center: true
                    })
                } else {
                    this.cateList = res.data.result
                    this.total = res.data.total
                }
            },
            handleSizeChange(newSize) {
                this.queryInfo.pagesize = newSize
                this.getCateList()
            },
            handleCurrentChange(newPage) {
                this.queryInfo.pagenum = newPage
                this.getCateList()
            },
            showAddCateDialog() {
                this.getParentCateList()
                this.addCateDialogVisible = true

            },
            // 获取父级分类列表
            async getParentCateList() {
                const res = (await this.$http.get('categories', {params: {type: 2}})).data
                if (res.meta.status !== 200) {
                    this.$message.error({
                        message: "获取父级分类列表失败",
                        center: true
                    })
                } else {
                    this.parentCateList = res.data
                    console.log(res);
                }
            },
            parentCateListChange() {
                if (this.selectedIds.length > 0) {
                    this.addCateForm.cat_pid = this.selectedIds[this.selectedIds.length - 1]
                    this.addCateForm.cat_level = this.selectedIds.length
                }
            },
            async addCate() {
                const res = (await this.$http.post('categories', this.addCateForm)).data
                if (res.meta.status !== 201) {
                    this.$message.error({
                        message: '添加分类失败！',
                        center: true
                    })
                } else {
                    this.$message.success({
                        message: '添加分类成功!',
                        center: true
                    })
                    await this.getCateList()
                    this.addCateDialogVisible = false
                }
            },
            addCateDialogClosed() {
                this.$refs.addCateFormRef.resetFields()
                this.selectedIds = []
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0
            },
            showEditCateDialog(info) {
                console.log(info);
                this.editCateDialogVisible = true
                this.editCateForm.cat_name = info.cat_name
                this.editCateForm.cat_id = info.cat_id
            },
            editCateDialogClosed() {
                this.$refs.editCateFormRef.resetFields()
            },
            async editCate() {
                const res = (await this.$http.put('categories/' + this.editCateForm.cat_id, {cat_name: this.editCateForm.cat_name})).data
                if (res.meta.status !== 200) {
                    this.$message.error({
                        message: '更新失败!',
                        center: true
                    })
                } else {
                    this.$message.success({
                        message: '更新成功!',
                        center: true
                    })
                    await this.getCateList()
                    this.editCateDialogVisible = false
                }
            },
            async deleteCate(info) {
                const message = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err)
                if (message == 'cancel') {
                    this.$message.info({
                        message:'已取消删除!',
                        center:true
                    })
                }else {
                    const res = (await this.$http.delete('categories/' + info.cat_id)).data
                    if(res.meta.status !== 200){
                        this.$message.error({
                            message:'删除失败!',
                            center:true
                        })
                    }else {
                        this.$message.success({
                            message:'删除成功!',
                            center:true
                        })
                        await this.getCateList()
                    }
                }
            }
        }
    }
</script>

<style scoped>

    .treeTable {
        margin-top: 15px;
    }

    .page {
        width: 580px;
        margin: 15px auto 0 auto;
    }
</style>